//
//  UseRulesViewController.m
//  tuanGG
//
//  Created by 李洪旭 on 15/11/19.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "UseRulesViewController.h"
#import "UserRulesTableViewCell.h"
@interface UseRulesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray * isSelectArray;
@property (nonatomic,strong)NSMutableArray * selectArray;//选中的cell
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * cellindexArray;
@end

@implementation UseRulesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isSelectArray = [[NSMutableArray alloc]init];
    _selectArray = [[NSMutableArray alloc]init];
    _cellindexArray = [[NSMutableArray alloc]init];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0efed"];
    [self addnavigationTitle:@"使用规则" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self addTableView];
}
- (void)initlizeDataSource {
    _dataArray = [[NSMutableArray alloc]initWithObjects:@"需要预约，请提前致电商家询问",@"无需预约，消费高峰时可能需要等位",@"店内无包间",@"可免费使用包间",@"可使用包间，包间费到店另算",@"可叠加使用，不兑现，不找零，超出部分到店另付",@"仅限食堂，不提供餐前外带，餐毕未吃完可免费打包",@"如需发票请提前致电商家询问",@"代金劵用户不可同时享受商家其他优惠",@"酒水饮料等问题，请致电商家咨询，以商家反馈为准",@"免费提供wifi",@"免费提供停车位",@"停车位收费标准，请提前致电商家询问", nil];
    
    for (int j = 0; j < _dataArray.count; j ++) {

        [_cellindexArray addObject:@(j)];
    }


    if ([[User_member instance].role isEqualToString:@""] ||[User_member instance].role==nil ) {
        //是选中
        for (int i = 0; i < _dataArray.count; i ++) {
            
            [_isSelectArray addObject:@(0)];
        }
    }
    else
    {
        
        
//        for (int i = 0; i < _dataArray.count; i ++) {
//            
//            [_isSelectArray addObject:@(0)];
//        }
        //是选中
        NSMutableArray * roleArray = [NSMutableArray arrayWithArray:[[User_member instance].role componentsSeparatedByString:@"a"]];
       
        for (int j = 0; j < _cellindexArray.count; j ++) {
            
            BOOL find=false;
            for (int i = 0; i < roleArray.count; i ++) {
                
                if ([roleArray[i] integerValue ] == [_cellindexArray[j]integerValue]) {
                    
                    [_isSelectArray addObject:@(1)];
                    find=true;
                    break;
                }
               
            }
            if (!find) {
                
                [_isSelectArray addObject:@(0)];
            }
            
        }
        
        
        
//        [_tableView reloadData];
    }
    
    
    
}
- (void)addTableView {
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = NO;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.backgroundColor = [UIColor colorWithHexString:@"f0efed"];
    tableView.tableFooterView = [[UITableView alloc]init];
    [tableView registerClass:[UserRulesTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:tableView];
    
    UIView *footButton = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 70)];
    
    UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 35, ScreenWidth-30, 45)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.layer.cornerRadius = 5;
    saveButton.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [saveButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    [footButton addSubview:saveButton];
    tableView.tableFooterView = footButton;
}
#pragma mark - 事件实现 
- (void)saveButton:(UIButton *)sender {
    NSLog(@"保存");
    [_selectArray removeAllObjects];
    for (int i = 0; i < _isSelectArray.count; i ++) {
        
        if ([_isSelectArray[i] integerValue] == 1) {
            //选中的数据家
            [_selectArray addObject:@(i)];
        }
    }
    [self NetWorking];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserRulesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
//    cell.textLabel.numberOfLines = 0;
//    cell.inforlabel.text = _dataArray[indexPath.row];
    [cell setCellWithInfor:_dataArray[indexPath.row]];
    BOOL isopen = [_isSelectArray[indexPath.row] boolValue];
    if (isopen) {
       [cell.selectedImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_checked")];
    }
    else
    {
     [cell.selectedImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_unchecked")];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BOOL isopen = [_isSelectArray[indexPath.row] boolValue];
    isopen = !isopen;
    [_isSelectArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:isopen]];
    [tableView reloadData];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self NetWorking];
    }
}
- (void)NetWorking {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
     NSString *newstr=[NSString string];
    if (_selectArray.count == 0) {
        
        [Common addAlert:@"请选中使用规则"];
        return;
    }
    else
    {
       
        for (int i= 0; i < _selectArray.count; i ++) {
            if (i > 0) {
                newstr = [_selectArray componentsJoinedByString:@"a"];
            }
            else
            {
                newstr = _selectArray[i];
            }
        }
        
        [params setObject:newstr forKey:@"content"];
    }
    [GLAFRequest startRequest:role_url paramsDic:params defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue ] == 1) {
            
            [Common addAlert:@"使用规则设置成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {
        
    }];
}

@end
